/**
 * Copyright 2011 The PlayN Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package football.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static playn.core.PlayN.*;

import playn.core.*;
import playn.core.util.Callback;


public class FootballGame extends Game.Default {


    private boolean loaded = false;

    private int countFilesToLoad;


    private GroupLayer boardLayer;

    public static final int UPDATE_RATE = 25;

    boolean soundEnabled=true;
    boolean pause = false;

    public FootballGame() {
        super(UPDATE_RATE);
    }


    Json.Object jmm;

    @Override
    public void init() {

        // createItem a group layer to hold the peas
        boardLayer = graphics().createGroupLayer();
        graphics().rootLayer().add(boardLayer);


        countFilesToLoad = 1;

        assets().getImage("images/sprite.png").addCallback(new Callback<Image>() {
            @Override
            public void onSuccess(Image image) {
                SpriteImages.setSpriteImage(image);
                countFilesToLoad--;
                if(countFilesToLoad==0)
                    onLoad();
            }

            @Override
            public void onFailure(Throwable err) {
                log().error("Error loading image!", err);
            }
        });

        setSoundEnable(true);


    }

    private Map<String,String> deathsParams = new HashMap<String, String>();
    private int lastSentDeath = 0;

    private void onLoad(){
        loaded = true;


        assets().getText("jmm/model.jmm",new Callback<String>() {
            @Override
            public void onSuccess(String json) {
                jmm = json().parse(json);
                if(jmm != null)
                    updateViewModel(jmm);
            }

            @Override
            public void onFailure(Throwable err) {
                log().error("Error loading model!", err);
            }
        });

        TextFormat labelFormat = new TextFormat().withFont(PlayN.graphics().createFont("DINCompProCondBold", Font.Style.PLAIN, 20));
        final GroupLayer rootLayer = graphics().rootLayer();



        final ImageLayer soundLayer = SpriteImages.createLayer(soundEnabled?Sprites.SoundOn:Sprites.SoundOff);
        soundLayer.setTranslation(610,10);
        graphics().rootLayer().add(soundLayer);
        soundLayer.addListener(new Pointer.Adapter() {
            @Override
            public void onPointerEnd(Pointer.Event event) {
                setSoundEnable(!soundEnabled);
                SpriteImages.toLayer(soundEnabled?Sprites.SoundOn:Sprites.SoundOff, soundLayer);

            }
        });

        initGame();
    }

    class ViewModel{
        Integer playerIndex = null;
        JsonMap<Player> Players
    }

    private void updateViewModel(Json.Object jmm){

    }


    float timee = 0;

    boolean needReset = false;

    public static String u="";

    @Override
    public void update(int delta) {
        if(!initialized) return;
        if(needReset){
            initialized = false;
            reset();
            needReset = false;
            return;
        }

        if(delta>500)
            delta=500;

        if(pause)
            return;

        timee+=delta;



    }

    @Override
    public void paint(float alpha) {
        if(!initialized) return;

        if(pause)
            return;

    }


    String username="";
    String useremail="";
    ISound sound;
    public void setSound(ISound sound) {
        this.sound = sound;
    }
    void setSoundEnable(boolean enable){
        soundEnabled=enable;
        if(soundEnabled){
            loopSound("music");
        }
        else{
            stopSound("music");
        }


    }

    public void playSound(String name){
        if(!soundEnabled) return;
        if(sound==null) sound=new CoreSound();
        sound.play(name);
    }
    public void loopSound(String name){
        if(!soundEnabled) return;
        if(sound==null) sound=new CoreSound();
        sound.loop(name);
        sound.volume(name,0.50f);
    }
    public void stopSound(String name){
        if(sound==null) sound=new CoreSound();
        sound.stop(name);
    }

    private boolean initialized = false;
    private void initGame(){
        if(!loaded)
            return;

        reset();
    }


    private void reset(){
        initialized = true;
    }





    int getScore(){
        return 0;
    }
}
